package com.polytech.dao;

import java.util.Date;
import java.util.List;
import java.util.Vector;

import com.polytech.model.Membre;
import com.polytech.model.Post;
import com.polytech.model.Topic;
import com.polytech.utils.Database;

public class PostDaoJdbc implements PostDao {

	private Database database;
	private MembreDao membreDao;
	public PostDaoJdbc(Database database) {
		this.database=database;
	}

	@Override
	public void insert(Post post,Membre poster,Topic topic) {
	}
	
	@Override
	public void insert(Post post, String userName, int idTopic) {
		database.insert("Post",new Object[]{null,post.getText(),post.getPublicationDate(),userName,idTopic});
	}
	
	@Override
	public Post select(int id) {
		Post post=null;
		Object[][] tab = database.select("Post",new Object[][]{{"id",id}});
		if(tab!=null){
			post=new Post(id,(String) tab[0][1],(Date)tab[0][2]);
			System.out.println(">>"+tab[0][3]);
			post.setPoster(new Membre((String) tab[0][3], "*"));
		}
		return post;
	}
	@Override
	public List<Post> findAllTopicPosts(int idTopic) {
		List<Post> res=null;
		Object[][] tab = database.select("Post",new String[][]{{"idTopic",""+idTopic}},new String[]{"id"});
		if(tab!=null){
			res=new Vector<>();
			for(int i=0;i<tab.length;i++){
				Post post=select((int) tab[i][0]);
				res.add(post);
			}
		}
		return res;
	}
	
	@Override
	public void update(Post value) {
		// TODO Auto-generated method stub

	}

	@Override
	public void delete(Post value) {
		// TODO Auto-generated method stub

	}

	public void setDatabase(Database database) {
		this.database = database;
	}

	public void setMembreDao(MembreDao membreDao) {
		this.membreDao = membreDao;
	}


	
}
